package com.xm.adv.activity;

import android.content.Context;
import android.hardware.Camera;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.xm.adv.R;

import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xinya.com.baselibrary.baseactivity.ZjbBaseActivity;
import xinya.com.baselibrary.router.RouterUrl;
import xinya.com.baselibrary.utils.DateTransforam;
import xinya.com.baselibrary.utils.ScreenUtils;
import xinya.com.baselibrary.utils.SdUtil;
import xinya.com.baselibrary.utils.VersionUtils;
@Route(path = RouterUrl.Info.info_info)
public class InfoActivity extends ZjbBaseActivity {

    @BindView(R.id.imageBack)
    ImageView imageBack;
    @BindView(R.id.textViewTitle)
    TextView textViewTitle;
    @BindView(R.id.textViewRight)
    TextView textViewRight;
    @BindView(R.id.line)
    TextView line;
    @BindView(R.id.viewBar)
    LinearLayout viewBar;
    @BindView(R.id.textVersion)
    TextView textVersion;
    @BindView(R.id.textDeviceVersion)
    TextView textDeviceVersion;
    @BindView(R.id.textSd)
    TextView textSd;
    @BindView(R.id.textDate)
    TextView textDate;
    @BindView(R.id.textLight)
    TextView textLight;
    @BindView(R.id.textVoice)
    TextView textVoice;
    @BindView(R.id.textMac)
    TextView textMac;
    @BindView(R.id.textActicve)
    TextView textActicve;
    @BindView(R.id.textShe)
    TextView textShe;
    private int maxVoice;
    private int currentVoice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {

    }

    @Override
    protected void initViews() {
        textViewTitle.setText("应用信息");
        textVersion.setText(VersionUtils.getCurrVersionName(mContext));
        textDeviceVersion.setText(Build.DEVICE);
        textSd.setText("机身：" + SdUtil.getRomAvailableSize(mContext) + "(可用)/" + SdUtil.getRomTotalSize(mContext) + "(总大小)");
        textDate.setText(DateTransforam.stampToDateYYMMdd(System.currentTimeMillis()));
        textLight.setText("当前亮度："+ScreenUtils.getScreenBrightness(this)+"\n最大亮度：255");
        AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        maxVoice = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
        currentVoice = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
        textVoice.setText("当前音量："+ currentVoice +"\n最大音量："+ maxVoice);
        textMac.setText(getMacFromHardware());
        textActicve.setText("未激活");
        int numberOfCameras = Camera.getNumberOfCameras();
        textShe.setText(numberOfCameras+"个摄像头");
    }

    @Override
    protected void initRecyclerview() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.imageBack)
    public void onViewClicked() {
        onBackPressed();
    }

    /**
     * 遍历循环所有的网络接口，找到接口是 wlan0
     * 必须的权限 <uses-permission android:name="android.permission.INTERNET" />
     * @return
     */
    private static String getMacFromHardware() {
        try {
            List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface nif : all) {
                if (!nif.getName().equalsIgnoreCase("wlan0")) {
                    continue;
                }

                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null) {
                    return "";
                }

                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:", b));
                }

                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                }
                return res1.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "02:00:00:00:00:00";
    }
}
